/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "timeline_srv_proxy.h"

#include <ipc_types.h>
#include <message_parcel.h>
#include <message_option.h>

#include "itimeline_callback.h"
#include "timeline_database_info.h"
#include "timeline_common.h"

namespace OHOS {
namespace Msdp {
void TimelineSrvProxy::Subscribe(const TimelineDataUtils::TimelineArea& type, const sptr<ITimelineCallback>& callback)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF((remote == nullptr) || (callback == nullptr));

    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGE(TIMELINE_MODULE_INNERKIT, "Write descriptor failed");
        return;
    }

    TIMELINE_WRITE_PARCEL_NO_RET(data, Int32, type);
    TIMELINE_WRITE_PARCEL_NO_RET(data, RemoteObject, callback->AsObject());

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_SUBSCRIBE), data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGE(TIMELINE_MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
        return;
    }
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Exit");
}

void TimelineSrvProxy::UnSubscribe(const TimelineDataUtils::TimelineArea& area,
    const sptr<ITimelineCallback>& callback)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF((remote == nullptr) || (callback == nullptr));

    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGE(TIMELINE_MODULE_INNERKIT, "Write descriptor failed!");
        return;
    }

    TIMELINE_WRITE_PARCEL_NO_RET(data, Int32, area);
    TIMELINE_WRITE_PARCEL_NO_RET(data, RemoteObject, callback->AsObject());

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_UNSUBSCRIBE), data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGE(TIMELINE_MODULE_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
        return;
    }
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Exit");
}

bool TimelineSrvProxy::SetPositionState(int32_t homevalue, int32_t companyvalue)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    bool result = false;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    TIMELINE_WRITE_PARCEL_WITH_RET(data, Int32, homevalue, false);
    TIMELINE_WRITE_PARCEL_WITH_RET(data, Int32, companyvalue, false);

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_SETPOSITIONSTATE),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadBool(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
    }

    return result;
}

bool TimelineSrvProxy::SetHomePosition(double longitude, double latitude)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    bool result = false;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    TIMELINE_WRITE_PARCEL_WITH_RET(data, Double, longitude, false);
    TIMELINE_WRITE_PARCEL_WITH_RET(data, Double, latitude, false);

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_SETHOMEPOSITION),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadBool(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
    }

    return result;
}

bool TimelineSrvProxy::SetCompanyPosition(double longitude, double latitude)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    bool result = false;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    TIMELINE_WRITE_PARCEL_WITH_RET(data, Double, longitude, false);
    TIMELINE_WRITE_PARCEL_WITH_RET(data, Double, latitude, false);

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_SETCOMPANYPOSITION),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadBool(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
    }

    return result;
}

bool TimelineSrvProxy::SetSleepTime(int32_t sleeptime, int32_t wakeuptime)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    bool result = false;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    TIMELINE_WRITE_PARCEL_WITH_RET(data, Int32, sleeptime, false);
    TIMELINE_WRITE_PARCEL_WITH_RET(data, Int32, wakeuptime, false);

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_SETSLEEPTIME),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadBool(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
    }

    return result;
}

bool TimelineSrvProxy::SetRestTime(int32_t resttimestart, int32_t resttimeend)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    bool result = false;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    TIMELINE_WRITE_PARCEL_WITH_RET(data, Int32, resttimestart, false);
    TIMELINE_WRITE_PARCEL_WITH_RET(data, Int32, resttimeend, false);

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_SETRESTTIME),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadBool(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
    }

    return result;
}

bool TimelineSrvProxy::SetWorkTime(int32_t worktimestart, int32_t worktimeend)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    bool result = false;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    TIMELINE_WRITE_PARCEL_WITH_RET(data, Int32, worktimestart, false);
    TIMELINE_WRITE_PARCEL_WITH_RET(data, Int32, worktimeend, false);

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_SETWORKTIME),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadBool(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
    }

    return result;
}

uint32_t TimelineSrvProxy::GetSleepTime()
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::GetSleepTime: fun is start!");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    uint32_t result = 0;
    double longitude = 0;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }
    TIMELINE_WRITE_PARCEL_WITH_RET(data, Double, longitude, false);
    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_GETSLEEPTIME), data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %{public}d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadUint32(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
        }

    return result;
}

uint32_t TimelineSrvProxy::GetWakeupTime()
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::GetWakeupTime: fun is start!");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    uint32_t result = 0;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_GETWAKEUPTIME),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %{public}d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadUint32(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
        }

    return result;
}

uint32_t TimelineSrvProxy::GetRestTimeStart()
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::GetRestTimeStart: fun is start!");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    uint32_t result = 0;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_GETSTARTRESTTIME),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %{public}d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadUint32(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
        }

    return result;
}

uint32_t TimelineSrvProxy::GetRestTimeEnd()
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::GetRestTimeEnd: fun is start!");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    uint32_t result = 0;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_GETENDRESTTIME),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %{public}d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadUint32(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
        }

    return result;
}

uint32_t TimelineSrvProxy::GetWorkTimeStart()
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::GetWorkTimeStart: fun is start!");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    uint32_t result = 0;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_GETSTARTWORKTIME),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %{public}d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadUint32(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
        }

    return result;
}

uint32_t TimelineSrvProxy::GetWorkTimeEnd()
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::GetWorkTimeEnd: fun is start!");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, false);

    uint32_t result = 0;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return result;
    }

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_GETENDWORKTIME),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %{public}d",
            __func__, ret);
        return result;
    }

    if (!reply.ReadUint32(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return result;
        }

    return result;
}

TimelineDatabaseInfo::ForcecastPosition TimelineSrvProxy::GetForcecast(uint32_t time)
{
    TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Enter");
    sptr<IRemoteObject> remote = Remote();
    TIMELINE_RETURN_IF_WITH_RET(remote == nullptr, TimelineDatabaseInfo::ForcecastPosition::UNKNOWN);

    uint32_t result = 0;
    MessageParcel data;
    MessageParcel reply;
    MessageOption option;

    if (!data.WriteInterfaceToken(TimelineSrvProxy::GetDescriptor())) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineClient::%{public}s write descriptor failed!", __func__);
        return TimelineDatabaseInfo::ForcecastPosition::UNKNOWN;
    }

    TIMELINE_WRITE_PARCEL_WITH_RET(data, Uint32, time, TimelineDatabaseInfo::ForcecastPosition::UNKNOWN);

    int ret = remote->SendRequest(static_cast<int>(ITimeline::TIMELINE_GETFORCECAST),
        data, reply, option);
    if (ret != ERR_OK) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "TimelineSrvProxy::%{public}s SendRequest is failed, error code: %d",
            __func__, ret);
        return TimelineDatabaseInfo::ForcecastPosition::UNKNOWN;
    }

    if (!reply.ReadUint32(result)) {
        TIMELINE_HILOGD(TIMELINE_MODULE_INNERKIT, "Readback fail!");
        return TimelineDatabaseInfo::ForcecastPosition::UNKNOWN;
    }

    return static_cast<TimelineDatabaseInfo::ForcecastPosition>(result);
}
} // Msdp
} // OHOS